package com.pxl.springbootinit.controller;

import cn.hutool.poi.excel.sax.SheetRidReader;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pxl.springbootinit.common.BaseResponse;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.common.ResultUtils;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.item.ItemAuditForm;
import com.pxl.springbootinit.model.dto.item.ItemForm;
import com.pxl.springbootinit.model.entity.Item;
import com.pxl.springbootinit.model.enums.ReviewStatusEnum;
import com.pxl.springbootinit.model.vo.ItemVO;
import com.pxl.springbootinit.model.vo.RankVO;
import com.pxl.springbootinit.model.vo.StatisticsItemVO;
import com.pxl.springbootinit.service.ItemService;
import org.apache.poi.ss.formula.functions.Rank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/items")
public class ItemController {

    @Resource
    private ItemService itemService;

    // 物品发布
    @PostMapping("/publish")
    public BaseResponse<Boolean> publishItem(@RequestBody ItemForm itemForm) {
        return ResultUtils.success(itemService.publishItem(itemForm));
    }

    /**
     * 物品展示与搜索
     * @param itemForm
     * @return
     */
    @GetMapping("/page")
    public BaseResponse<Page<ItemVO>> itemPage(ItemForm itemForm)  {
        return ResultUtils.success(itemService.itemPage(itemForm));
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateItem(@RequestBody ItemForm itemForm) {
        return ResultUtils.success(itemService.updateItem(itemForm));
    }

    @PostMapping("/remove/{id}")
    public BaseResponse<Boolean> removeItem(@PathVariable Long id) {
        return ResultUtils.success(itemService.removeById(id));
    }

    @GetMapping("/{id}")
    public BaseResponse<ItemVO> getItem(@PathVariable Long id) {
        return ResultUtils.success(itemService.getItemVO(itemService.getById(id)));
    }

    /**
     * 返回所有商品种类
     * @return
     */
    @GetMapping("/types")
    public BaseResponse<List<String>> itemTypes() {
        return ResultUtils.success(itemService.itemTypes());
    }


    @GetMapping("/pictures")
    public BaseResponse<List<String>> itemPictures(HttpServletRequest request) throws IOException, URISyntaxException {
        return ResultUtils.success(itemService.itemPictures(request));
    }

    @GetMapping("/rank/{nums}")
    public BaseResponse<List<RankVO>> rank(@PathVariable Integer nums) {
        return ResultUtils.success(itemService.rank(nums));
    }


    /**
     * 返回捐赠中心总统计数据
     */
    @GetMapping("/statistics")
    public BaseResponse<StatisticsItemVO> statistics() throws ExecutionException, InterruptedException {
        return ResultUtils.success(itemService.statistics());
    }

    // region 管理员操作

    @PostMapping("/admin/pass/{id}")
    public BaseResponse<Boolean> passExamineItem(@PathVariable Long id) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Item item = itemService.getById(id);
        if (item == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"物品不存在");
        }
        item.setStatus(ReviewStatusEnum.APPROVED.getValue());
        return ResultUtils.success(itemService.updateById(item));
    }

    @PostMapping("/admin/reject/{id}")
    public BaseResponse<Boolean> rejectExamineItem(@PathVariable Long id) {
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Item item = itemService.getById(id);
        if (item == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"物品不存在");
        }
        item.setStatus(ReviewStatusEnum.REJECTED.getValue());
        return ResultUtils.success(itemService.updateById(item));
    }


    /**
     * 返回所有商品种类
     * @return
     */
    @GetMapping("/admin/types")
    public BaseResponse<List<String>> itemAllTypes() {
        return ResultUtils.success(itemService.itemAllTypes());
    }


    @GetMapping("/admin/page")
    public BaseResponse<Page<ItemVO>> adminPage(ItemForm itemForm) {
        return ResultUtils.success(itemService.adminPage(itemForm));
    }

    /**
     * 返回系统总物品数量
     * @return
     */
    @GetMapping("/admin/count")
    public BaseResponse<Long> getUserCount() {
        Long count = itemService.count();
        return ResultUtils.success(count);
    }
    //endregion

}
